package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;

import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    /**
     * 上传科室接口
     *
     * @param paramMap
     */
    @Override
    public void save(Map<String, Object> paramMap) {
        // 把paramMap转换为Department对象
        String mapString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(mapString, Department.class);

        // 根据hoscode depcode查询
        String hoscode = department.getHoscode();
        String depcode = department.getDepcode();
        Department departmentExits = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);

        // 判断是否存在
        if (departmentExits != null) {
            // 如果存在 修改
            department.setId(departmentExits.getId());
            department.setCreateTime(departmentExits.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        } else {
            // 如果不存在 添加
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    /**
     * 查询科室接口
     *
     * @param page
     * @param limit
     * @param departmentQueryVo
     * @return
     */
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        // 创建pageable对象 设置当前页和每页显示条数 0是第一页
        Pageable pageable = PageRequest.of(page - 1, limit);

        // 把departmentQueryVo转换为department
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);

        // 创建ExampleMatcher匹配规则
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) // 根据字符串匹配模糊查询
                .withIgnoreCase(true); // 忽略大小写
        // 创建Example对象 模糊查询
        Example<Department> example = Example.of(department, matcher); // 传入对象 匹配规则

        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    /**
     * 删除科室接口
     *
     * @param hoscode
     * @param depcode
     */
    @Override
    public void remove(String hoscode, String depcode) {
        // 根据hoscode depcode 查询信息
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        // 如果存在 删除
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    /**
     * 根据医院编号查询所有科室列表
     *
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        // 创建list集合 用于最终数据封装
        List<DepartmentVo> result = new ArrayList<>();

        // 根据医院编号 查询医院所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hoscode);
        Example example = Example.of(departmentQuery);
        // 所有科室列表信息
        List<Department> departmentList = departmentRepository.findAll(example);

        // 根据大科室编号 bigcode 分组 然后获取每个大科室中的下级节点子科室
        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        // 遍历map集合 departmentMap
        for (Map.Entry<String, List<Department>> entry : departmentMap.entrySet()) {
            // 大科室编号
            String bigcode = entry.getKey();
            // 大科室编号对应的全部小科室
            List<Department> deptList = entry.getValue();

            // 封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(bigcode);
            departmentVo1.setDepname(deptList.get(0).getBigname());

            // 封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : deptList) {
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                // 封装到list集合中
                children.add(departmentVo2);
            }
            // 把小科室list集合放入大科室children中
            departmentVo1.setChildren(children);
            // 放入最终result集合中
            result.add(departmentVo1);
        }
        // 返回
        return result;
    }

    /**
     * 根据医院编号 科室编号 查询科室名称
     *
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            return department.getDepname();
        }
        return null;
    }

    // 根据医院编号 科室编号 查询科室
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
